// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.quartz.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.QuartzTask;
import com.liuxinlong.modules.quartz.controller.request.QueryQuartzJobReq;
import com.liuxinlong.modules.quartz.controller.request.UpdateQuartzJobReq;
import com.liuxinlong.modules.quartz.service.QuartzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年7月6日
 */
@Slf4j
@RestController
@RequestMapping(path = "/quartz")
public class QuartzController {
    @Autowired
    private QuartzService quartzService;

    /**
     * 定时任务查询列表
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/page")
    @ResponseBody
//    @LogAnnotation(value ="分页查询",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageQuartzJobs(@RequestBody QueryQuartzJobReq req) {
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("name",req.getName());
        queryParam.put("startNum",req.getStartNum());
        queryParam.put("pageSize",req.getPageSize());
        List<Map<String,Object>> resultList = quartzService.pageQuartzJobs(queryParam);
        int totalNum = quartzService.getQuartzJobsCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(resultList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询定时任务列表成功");
        return resp;
    }

    /**
     * 新增定时任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/add")
    @ResponseBody
    @LogAnnotation(value ="新增",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addJob(@RequestBody UpdateQuartzJobReq req) {
        QuartzTask jobInfo = new QuartzTask();
        jobInfo.setJobName(req.getJobName());
        jobInfo.setJobGroup(req.getJobGroup());
        jobInfo.setMethodName(req.getMethodName());
        jobInfo.setJobClass(req.getJobClass());
        jobInfo.setCron(req.getCron());
        jobInfo.setSpringId(req.getSpringId());
        jobInfo.setRemark(req.getRemark());
        quartzService.addTask(jobInfo);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("添加任务成功");
        return resp;
    }

    /**
     * 新增定时任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/update")
    @ResponseBody
    @LogAnnotation(value ="修改",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateJob(@RequestBody UpdateQuartzJobReq req) {
        QuartzTask jobInfo = new QuartzTask();
        jobInfo.setId(req.getId());
        jobInfo.setJobName(req.getJobName());
        jobInfo.setJobGroup(req.getJobGroup());
        jobInfo.setMethodName(req.getMethodName());
        jobInfo.setJobClass(req.getJobClass());
        jobInfo.setCron(req.getCron());
        jobInfo.setSpringId(req.getSpringId());
        jobInfo.setRemark(req.getRemark());
        quartzService.updateTask(jobInfo);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改任务成功");
        return resp;
    }

    /**
     * 启用定时任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/enable")
    @ResponseBody
    @LogAnnotation(value ="启用",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp enableJob(@RequestBody BaseDeleteReq req) {
        quartzService.enableJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务启用成功");
        return resp;
    }

    /**
     * 启动定时任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/start")
    @ResponseBody
    @LogAnnotation(value ="启动",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp startJob(@RequestBody BaseDeleteReq req) {
        quartzService.startJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务启动成功");
        return resp;
    }

    /**
     * 暂停任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/pause")
    @ResponseBody
    @LogAnnotation(value ="暂停任务",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp pauseJob(@RequestBody BaseDeleteReq req) {
        quartzService.pauseJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务暂停成功");
        return resp;
    }

    /**
     * 挂起任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/suspend")
    @ResponseBody
    @LogAnnotation(value ="挂起任务",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp suspendJob(@RequestBody BaseDeleteReq req) {
        quartzService.suspendJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务挂起成功");
        return resp;
    }

    /**
     * 恢复任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/resume")
    @ResponseBody
    @LogAnnotation(value ="恢复",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp resumeJob(@RequestBody BaseDeleteReq req) {
        quartzService.resumeJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务恢复成功");
        return resp;
    }

    /**
     * 重启任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/reschedule")
    @ResponseBody
    @LogAnnotation(value ="重启",business = LogBusinessConstants.QUARTZ_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp rescheduleJob(@RequestBody BaseDeleteReq req) {
        quartzService.rescheduleJob(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("定时任务重启成功");
        return resp;
    }

    /**
     * 删除任务
     *
     * @param req 请求体
     * @return BaseResp
     */
    @PostMapping(path = "/delete")
    @ResponseBody
    public BaseResp deleteJob(@RequestBody BaseDeleteReq req) {
        quartzService.deleteJob(req.getId());
        BaseResp resp = new BaseResp();
        return resp;
    }

    /**
     * 查询任务
     *
     * @param pageNum  页码
     * @param pageSize 每页显示多少条数据
     * @return Map
     */
    @GetMapping(path = "/query")
    @ResponseBody
    public BaseResp query(Integer pageNum, Integer pageSize) {
//        PageInfo<JobAndTriggerDto> pageInfo = quartzService.getJobAndTriggerDetails(pageNum, pageSize);
//        Map<String, Object> map = new HashMap<>();
//        if (!StringUtils.isEmpty(pageInfo.getTotal())) {
//            map.put("JobAndTrigger", pageInfo);
//            map.put("number", pageInfo.getTotal());
//            return new ResultMap().success().data(map).message("查询任务成功");
//        }
//        return new ResultMap().fail().message("查询任务成功失败，没有数据");
        BaseResp resp = new BaseResp();
        return resp;
    }
}
